//
//  UICollectionView+PagingEnabled.m
//  lvfj
//
//  Created by lvfeijun on 2021/7/1.
//  Copyright © 2021 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "UICollectionView+PagingEnabled.h"

@implementation UICollectionView (PagingEnabled)
/*
 [self.rightCollectionView registerClass:[SHCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SHCollectionReusableViewHeader];

 //使用原有重用视图
 [self.rightCollectionView registerClass:[UICollectionReusableView class]  forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SHCollectionReusableViewFooter ];
 */
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableview = nil;

    if (kind == UICollectionElementKindSectionHeader) {

        SHCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:SHCollectionReusableViewHeader forIndexPath:indexPath];
        /**
         *
         * 注意:虽然这里没有看到明显的initWithFrame方法,但是在获取重用视图的时候,系统会自动调用initWithFrame方法的.所以在initWithFrame里面进行初始化操作,是没有问题的!
         */

        [headerView getSHCollectionReusableViewHearderTitle:hearderTitleArray[indexPath.section]];

        reusableview = headerView;
    }

    if (kind == UICollectionElementKindSectionFooter) {

        UICollectionReusableView *footerview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:SHCollectionReusableViewFooter forIndexPath:indexPath];
        /**
         *   如果头尾视图没什么很多内容,直接创建对应控件进行添加即可,无需自定义.
         */
        footerview.backgroundColor=[UIColor redColor];
        reusableview = footerview;
    }

    return reusableview;
}

- (UICollectionView *)collectView{
    if (!_collectView) {
        CGFloat kScreen_Width = 480;
        CGFloat w = (kScreen_Width-10-15*2)/2;
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(w, w/3);
        //横向
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //横行间距
        flowLayout.minimumLineSpacing = 15;
        //竖列间距
        flowLayout.minimumInteritemSpacing = 20;
        //间距 //横向翻页左右不留间距，竖直翻页上下不留间距，否则设置pagingEnabled的时候会有偏移，因为差了一个间距的距离
        flowLayout.sectionInset = UIEdgeInsetsMake(15, 0, 0, 0);
        //size
        _collectView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, 180) collectionViewLayout:flowLayout];
        _collectView.delegate = self;
        _collectView.dataSource = self;
        _collectView.backgroundColor = [UIColor clearColor];
        //分页
        _collectView.pagingEnabled = YES;
        _collectView.showsVerticalScrollIndicator = NO;
        _collectView.showsHorizontalScrollIndicator = NO;
//        [_collectView registerClass:HQHomeTagsItemCell.class forCellWithReuseIdentifier:@"HQHomeTagsItemCell"];
        [self addSubview:_collectView];
    }
    return _collectView;
}

-(NSArray *)dealData:(NSArray *)reArray{
    //一页十个数据
    NSInteger pageCount = 10.0;
    // 为了pagingEnabled页面的完整性（数据不重复，必须偷偷填充数据到一个一页的倍数）
    int total = ceil(reArray.count/pageCount);
    NSMutableArray *temp = [NSMutableArray arrayWithArray:reArray];
    for (NSInteger i=reArray.count; i<total*10; i++) {
        [temp addObject:@""];
    }
    
    NSMutableArray *all = [NSMutableArray array];
    while (temp.count>=pageCount) {
        //1 3 5 7 9
        //2 4 6 8 10
        //数据都是从上到下的，要变成从左到右
        //1 2 3 4 5
        //6 7 8 9 10
        [temp exchangeObjectAtIndex:1 withObjectAtIndex:2];
        [temp exchangeObjectAtIndex:4 withObjectAtIndex:1];
        [temp exchangeObjectAtIndex:3 withObjectAtIndex:6];
        [temp exchangeObjectAtIndex:1 withObjectAtIndex:8];
        [temp exchangeObjectAtIndex:1 withObjectAtIndex:5];
        [temp exchangeObjectAtIndex:5 withObjectAtIndex:7];
        
        NSRange range = NSMakeRange(0, pageCount);
        [all addObjectsFromArray:[temp subarrayWithRange:range]];
        [temp removeObjectsInRange:range];
    }
    if (temp.count) {
        [all addObjectsFromArray:temp];
    }
    return all;
}

@end
